<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="请输入内容" class="input-with-select"  clearable v-model="queryData.query" @clear="getGoods">
          <el-button slot="append" icon="el-icon-search" @click="getGoods"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
      </el-col>
      </el-row>
      <el-table border stripe :data="List">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="100px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="100px"></el-table-column>
        <el-table-column label="创建时间" width="180px">
          <template slot-scope="scope">
            {{ scope.row.add_time | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
        <!-- 需要作用域插槽获取整行代码用于编辑和删除 -->
        <template>
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.pagenum"
      :page-sizes="[2, 8, 12, 16]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      //  总列表
      List: [],
      total: 0
    }
  },

  created () {
    this.getGoods()
  },

  methods: {
    async getGoods () {
      const { data: res } = await this.axios.get('goods', {
        params: this.queryData
      })
      //   console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.List = res.data.goods
      this.total = res.data.total
    },
    //  快速搜索
    //  每页的条数发生变化
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getGoods()
    },
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoods()
    }
  }
}
</script>

<style scoped lang='less'>

</style>
